package com.csyf.manager.thread;

import com.csyf.manager.channel.base.ServiceEnum;
import com.csyf.manager.entity.SysTrade;
import com.csyf.manager.service.OrderBiz;
import com.csyf.manager.service.OrderService;
import com.csyf.manager.service.PayMentBiz;
import com.csyf.manager.service.impl.RedisService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicInteger;

@Slf4j
@Service
public class DySchedule {


    private static final int pagesize = 40;


    @Autowired
    private RedisService redisService;

    @Autowired
    private OrderService orderService;

    @Autowired
    private OrderBiz orderBiz;

    @Autowired
    private PayMentBiz payMentBiz;

    @Autowired
    private Environment env;

    private static AtomicInteger line = new AtomicInteger(0);
    static ExecutorService pool = Executors.newFixedThreadPool(100);

    public static int getLine() {
        return line.addAndGet(pagesize);
    }

    public void doJob() {
        for (int i = 0; i < 100; i++) {
            TradeThread thread = new TradeThread();
            pool.execute(thread);
        }
//        pool.shutdown();

    }


    /**
     * @Author: Enzo
     * @Date: 2018/5/30 14:46
     */
    private class TradeThread extends Thread {


        @Override
        public void run() {
            System.out.println("线程:" + Thread.currentThread().getName());
            Integer num = DySchedule.getLine();
            System.out.println("startline = " + (num - pagesize) + ",endline = " + num);


            String[] activeProfiles = env.getActiveProfiles();
            List<String> strings = Arrays.asList(activeProfiles);

            log.info("scheduled 2: {} ,profile: {}", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()), env.getActiveProfiles());

            List<SysTrade> sysTrades = orderService.queryWaitOrder(num / pagesize - 1, pagesize);

            for (SysTrade sysTrade : sysTrades) {
                Date createTime = sysTrade.getCreateTime();
                Date curDate = new Date();
                //如果当前时间和创建时间 小于2min 则不进行查询;若当前时间和创建时间 大于2小时，执行默认订单查询功能。
                if (curDate.getTime() - createTime.getTime() < 120000) {
                    continue;
                } else if(curDate.getTime() - createTime.getTime() > 7200000) {
                    defaultprocess(sysTrade);
                    continue;
                }

                try {
//                    ServiceEnum serviceEnum = ServiceEnum.valueOf(sysTrade.getServiceType());
//                    switch (serviceEnum) {
//                        case YOP_WEB:
//                        case FFT_UNION_SM:
//                        case PAY_WECHAT_TM:
//                        case HFTX_WEB_PAY:
//                        case HFTX_H5_PAY:
//                        case SAND_FAST:
//                        case SAND_WEB:
//                        case PA_WEIXIN_SM:
//                        case TDF_ALI_SM:
//                            if (strings.contains("prod")) {
//                                orderBiz.processOrder(sysTrade);
//                            }
//                            break;
//                        default:
//                            defaultprocess(sysTrade);
//                            break;
//
//                    }
                    if (strings.contains("prod")) {
                        orderBiz.processOrder(sysTrade);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }


        }
    }

    /**
     * 默认执行（当没写查询订单）
     */
    public void defaultprocess(SysTrade sysTrade) {
        orderBiz.processOrder(sysTrade);
    }








}